vm = new Vue({
    el: '#app',  //选中整个main
    data: {
        goodsPage: {},
        hotGoodsList: {},
        types: {},
        typeName: '',
        typeId: sessionStorage.getItem('typeId'),
        goodsName: ''
    },
    methods: {
        exitLogin:function(){
            axios.get("user/exitLogin").then(rs=>{
                sessionStorage.removeItem("user");
                sessionStorage.removeItem("showNum");
                sessionStorage.removeItem("typeId");
                layer.msg("退出登录成功！");
                location.href="index.html";
            });
        },
        getGoods: function () {
            axios.get("goods/getHotGoods").then(rs => {
                this.hotGoodsList = rs.data.obj;
            })
        },
        getAllGoods: function (start = 1, pageSize = 12) {
            if (this.typeId == 0 || this.typeId == '' || this.typeId == undefined) {
                let item = sessionStorage.getItem("goodsName");
                if (item != '' && item != undefined && item != null) {
                    axios.get(`goods/getGoodsPageByGoodsName/${start}/${pageSize}/${item}`).then(rs => {
                        this.goodsPage = rs.data.obj;
                        this.typeName = '全部列表';
                    })
                } else {
                    axios.get(`goods/getGoodsPage/${start}/${pageSize}`).then(rs => {
                        this.goodsPage = rs.data.obj;
                        this.typeName = '全部列表';
                    })
                }

            } else {
                axios.get(`goods/getGoodsPageByGoodsId/${start}/${pageSize}/${this.typeId}`).then(rs => {
                    this.goodsPage = rs.data.obj;
                })
            }
        },
        //商品详情
        toDetail: function (goods, event) {
            // event.preventDefault();
            axios.put(`goods/updateLookById/${goods.id}/${goods.look}`).then(rs => {
                goods.look = goods.look + 1;
                layer.msg(rs.data.msg);
                sessionStorage.setItem('goods', JSON.stringify(goods));
                location.href = 'goodsDetail.html';
            }).catch(e => {
                layer.msg(e.message)
            });
        },
        //获取商品列表
        getAllGoodsType: function () {
            axios.get('type/getAllGoodsType').then(rs => {
                this.types = rs.data.obj;
                for (let i = 0; i < this.types.length; i++) {
                    if (this.typeId == this.types[i].id) {
                        this.typeName = this.types[i].goodsType;
                    }
                }
                this.getAllGoods();
            });
        },
        //列表转换
        toGoodsListByType: function (typeId) {
            sessionStorage.setItem("typeId", typeId);
            sessionStorage.setItem("goodsName", "");
            location.href = 'productList.html';
        },

        //搜索，模糊查询
        search: function () {
            sessionStorage.setItem("typeId", 0);
            sessionStorage.setItem("goodsName", this.goodsName);
            location.href = 'productList.html';
        },
        getUserInfo:function () {
            let user = sessionStorage.getItem("user");
            if (user==''&&user==undefined&&user==null) {
                axios.get('user/getUserInfo').then(rs => {
                    let user = rs.data.obj;
                    sessionStorage.setItem("user", JSON.stringify(user));
                    sessionStorage.setItem("showNum", 5);
                    sessionStorage.setItem("typeId", 0);
                    location.href = 'home.html';
                }).catch(e => {
                    layer.msg(e.message);
                })
            }
            // this.user=JSON.parse(sessionStorage.getItem("user"));
        }
    },
    created: function () {
        this.getUserInfo();
    },
    mounted:function () {
        this.getGoods();
        this.getAllGoodsType();
        this.getAllGoodsType();
    }
});